package com.ev.framework.utils;

import java.math.BigDecimal;
import java.math.BigInteger;

public class MathUtils {
	public static BigDecimal getBigDecimal(Object value) {
		BigDecimal ret = null;
		if (value != null) {
			if (value instanceof BigDecimal) {
				ret = (BigDecimal) value;
			} else if (value instanceof String) {
				ret = new BigDecimal((String) value);
			} else if (value instanceof BigInteger) {
				ret = new BigDecimal((BigInteger) value);
			} else if (value instanceof Number) {
				ret = new BigDecimal(((Number) value).doubleValue());
			} else {
				throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
						+ " into a BigDecimal.");
			}
		}
		return ret;
	}

	public static BigDecimal getBigDecimalByZero(Object value) {
		BigDecimal ret = BigDecimal.ZERO;
		if (value != null) {
			if (value instanceof BigDecimal) {
				ret = (BigDecimal) value;
			} else if (value instanceof String) {
				ret = new BigDecimal((String) value);
			} else if (value instanceof BigInteger) {
				ret = new BigDecimal((BigInteger) value);
			} else if (value instanceof Number) {
				ret = new BigDecimal(((Number) value).doubleValue());
			} else {
				throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
						+ " into a BigDecimal.");
			}
		}
		return ret;
	}

	public static boolean compareObject(Object v1,Object v2){
		if (v1 == null && v2 == null) {
			return true;
		} else {
			if (v1 != null && v2 != null) {
				if (v1 instanceof BigDecimal && v2 instanceof BigDecimal) {
					if (((BigDecimal) v1).compareTo((BigDecimal) v2) == 0) {
						return true;
					}
				} else if (v1 instanceof Integer && v2 instanceof Integer) {
					if (((int) v1)==((int) v2)) {
						return true;
					}
				} else {
					if (v1.equals(v2)) {
						return true;
					}
				}
			}
		}
		return false;
	}

}
